const { Workbook } = require("exceljs");
const address = require('./address');
const path = require("path");
var countArr = [];
var flag = false;
var newCount = 0;
async function main() {
  const workbook = new Workbook();
  const inpObj = {}; // 用于存储所有键值对的对象
  const selectObj = {}; // 用于存储所有键值对的对象
  const filePath = path.join(__dirname, "./test3.xlsx");
  const workbook2 = await workbook.xlsx.readFile(filePath);

  workbook2.eachSheet((sheet, index1) => {
    if (index1 == 1) {
      sheet.eachRow((row, index2) => {
        if (index2 === 1 && !flag) {
          row.eachCell((cell, colNumber) => {
            if (cell.value.includes('取值')) {
              newCount += 1; //count是取值的这一列出现的次数
              countArr.push(colNumber);
            }
          });
          flag = true;
          return; // 跳过第一行，因为它包含标题
        }

        var value = row.getCell(countArr[0]).text;
        console.log("🚀 ~ sheet.eachRow ~ value:", value)
        // 处理地区
        address.forEach((item, index) => {
          // 法人代表/自然人国籍 拼接英文
          if (item.includes(value) && row.getCell(3).value && row.getCell(3).value.trim() == '#ext-gen445,#lawyerCountry') {
            value = item;
          }
          if (item.includes(value) && row.getCell(3).value && row.getCell(3).value.trim() == '#ext-gen511,#merCountry') {
            // value = item.replace(/[\u4e00-\u9fa5]/g, '');
            value = item
          }
        });

        if (value !== undefined && typeof value === "number") {
          value = value.toString();
        } else {
          value = value;
        }
        if (row.getCell(2).value != null) {
          inpObj[row.getCell(2).value.trim()] = value; // 第 2 列作为键，第 6 列作为值
        }
        if (row.getCell(3).value != null) {
          selectObj[row.getCell(3).value.trim()] = value; // 第 3 列作为键，第 6 列作为值
        }
        const keyValues = {
          phones: ['#ext-comp-1082', '#lawyerPhone', '#realLawyerPhone'],
          addresses: ['#ext-gen562,#province', '#ext-gen425,#regAddrProvId']
        };

        let storeFlags = {
          phones: false,
          addresses: false
        };

        let storedValues = {
          phones: null,
          addresses: null
        };
        // storeFn(inpObj,storeFlags,storedValues) 
        // storeFn(selectObj,storeFlags,storedValues) 
        function storeFn(matchObj, flag, storedValues) {
          for (const key in matchObj) {
            for (const type in keyValues) {
              keyValues[type].forEach((item) => {
                if (key.includes(item) && matchObj[key] !== null && !flag[type]) {
                  storedValues[type] = matchObj[key];
                  flag[type] = true;
                }
                if (flag[type] && key.includes(item)) {
                  matchObj[key] = storedValues[type];
                  console.log("🚀 ~ keyValues[type].forEach ~ storedValues[type]:", storedValues[type])
                }
              });
            }
          }
        }
      });
    }
  });

  if (countArr.length > 0) {
    countArr.shift(); // 移除已经处理过的列索引
    console.log(countArr + '---' + newCount); // 输出处理后的 countArr 和 newCount
    return [inpObj, selectObj, newCount];
  } else {
    return "已处理完所有列索引";
  }
}

module.exports = main